package com.example.yichuangyun.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemDetailsContactBinding;
import com.example.yichuangyun.databinding.ItemDetailsContactWayBinding;
import com.example.yichuangyun.model.contacts.ContactPersonModel;
import com.example.yichuangyun.model.contacts.ContactWayModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.UiUtils;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("NotifyDataSetChanged")
public class DetailsContactAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private List<ContactPersonModel> mData;
    private OnCallListener onCallListener;

    public DetailsContactAdapter(Context context, List<ContactPersonModel> mData) {
        this.context = context;
        this.mData = mData;
    }

    public void onRefresh(List<ContactPersonModel> mList) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    public void addData(List<ContactPersonModel> mList) {
        if (mList != null && mList.size() != 0) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDetailsContactBinding itemDetailsContactBinding = ItemDetailsContactBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(itemDetailsContactBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            ContactPersonModel model = mData.get(position);
            myViewHolder.binding.ivSex.setImageResource(model.getSex() == 1 ? R.mipmap.icon_boy : model.getSex() == 2 ? R.mipmap.icon_girl : R.mipmap.icon_x);
            myViewHolder.binding.tvContactName.setText(CommonUtil.checkText(model.getName()));
            myViewHolder.binding.tvDef.setVisibility(model.getIsDef() == 1 ? View.VISIBLE : View.GONE);
            myViewHolder.binding.recyclerView.setAdapter(new ContactWayAdapter(mData.get(position).getTels()));
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemDetailsContactBinding binding;

        public MyViewHolder(ItemDetailsContactBinding itemDetailsContactBinding) {
            super(itemDetailsContactBinding.getRoot());
            binding = itemDetailsContactBinding;
        }
    }


    class ContactWayAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final List<ContactWayModel> contactWayData;

        public ContactWayAdapter(List<ContactWayModel> contactWayData) {
            this.contactWayData = contactWayData;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemDetailsContactWayBinding itemDetailsContactWayBinding = ItemDetailsContactWayBinding.inflate(LayoutInflater.from(context), parent, false);
            return new ContactWayViewHolder(itemDetailsContactWayBinding);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            if (holder instanceof ContactWayViewHolder) {
                ContactWayViewHolder contactWayViewHolder = (ContactWayViewHolder) holder;
                ContactWayModel model = contactWayData.get(position);
                contactWayViewHolder.binding.tvContactWay.setText(UiUtils.getContactTypeName(model.getType()));
                if (position > 0 && model.getType() == 1 && model.getType() == contactWayData.get(position - 1).getType()) {
                    contactWayViewHolder.binding.tvContactWay.setVisibility(View.GONE);
                } else {
                    contactWayViewHolder.binding.tvContactWay.setVisibility(View.VISIBLE);
                }
                if (position != contactWayData.size() - 1 && model.getType() == 1 && model.getType() == contactWayData.get(position + 1).getType()) {
                    contactWayViewHolder.binding.line.setVisibility(View.GONE);
                } else {
                    contactWayViewHolder.binding.line.setVisibility(View.VISIBLE);
                }
                contactWayViewHolder.binding.tvContactInfo.setText(CommonUtil.checkText(model.getInfo()));
                if (model.getType() == 1 || model.getType() == 5) {
                    contactWayViewHolder.binding.tvContactInfo.setTextColor(0xFF426FFF);
                } else {
                    contactWayViewHolder.binding.tvContactInfo.setTextColor(0xFF3B3A40);
                }
                contactWayViewHolder.binding.tvContactInfo.setOnClickListener(view -> {
                    if (model.getType() == 1 || model.getType() == 5) {
                        if (onCallListener != null && !TextUtils.isEmpty(model.getInfo())) {
                            onCallListener.callListener(model.getInfo());
                        }
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return contactWayData.size();
        }

        class ContactWayViewHolder extends RecyclerView.ViewHolder {

            ItemDetailsContactWayBinding binding;

            public ContactWayViewHolder(ItemDetailsContactWayBinding itemDetailsContactWayBinding) {
                super(itemDetailsContactWayBinding.getRoot());
                binding = itemDetailsContactWayBinding;
            }
        }
    }

    public interface OnCallListener {
        void callListener(String number);
    }

    public void setOnCallListener(OnCallListener onCallListener) {
        this.onCallListener = onCallListener;
    }
}
